package com.pioneer.controller.sale;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.pioneer.common.annotation.Log;
import com.pioneer.common.core.controller.BaseController;
import com.pioneer.common.core.domain.AjaxResult;
import com.pioneer.common.enums.BusinessType;
import com.pioneer.web.sale.domain.SalContract;
import com.pioneer.web.sale.service.ISalContractService;
import jakarta.annotation.Resource;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * 销售合同
 *
 * @author hlm
 */
@RestController
@RequestMapping("/sale/contract")
public class SalContractController extends BaseController {

    @Resource
    private ISalContractService contractService;

    /**
     * 获取销售合同列表
     *
     * @param contract 查询条件
     * @return 结果
     */
    @PreAuthorize("@ss.hasPerm('sale:contract:list')")
    @GetMapping("/list")
    public AjaxResult list(SalContract contract) {
        boolean isPage = startPage();
        List<SalContract> list = contractService.list(Wrappers.query(contract));
        return success(isPage, list);
    }

    /**
     * 根据销售合同id获取详细信息
     *
     * @param contractId 销售合同id
     * @return 结果
     */
    @PreAuthorize("@ss.hasPerm('sale:contract:query')")
    @GetMapping(value = "/{contractId}")
    public AjaxResult getInfo(@PathVariable Long contractId) {
        return AjaxResult.success(contractService.getContractById(contractId));
    }

    /**
     * 新增销售合同
     *
     * @param contract 销售合同
     * @return 结果
     */
    @PreAuthorize("@ss.hasPerm('sale:contract:add')")
    @Log(title = "销售合同", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody SalContract contract) {
        return toAjax(contractService.addContract(contract));
    }

    /**
     * 修改销售合同
     *
     * @param contract 销售合同
     * @return 结果
     */
    @PreAuthorize("@ss.hasPerm('sale:contract:edit')")
    @Log(title = "销售合同", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody SalContract contract) {
        return toAjax(contractService.editContract(contract));
    }

    /**
     * 删除销售合同
     *
     * @param contractIds 销售合同id数组
     * @return 结果
     */
    @PreAuthorize("@ss.hasPerm('sale:contract:remove')")
    @Log(title = "销售合同", businessType = BusinessType.DELETE)
    @DeleteMapping("/{contractIds}")
    public AjaxResult remove(@PathVariable Long[] contractIds) {
        return toAjax(contractService.removeContract(Arrays.asList(contractIds)));
    }

    /**
     * 提交销售合同
     *
     * @param contractIds 销售合同ID数组
     * @return 结果
     */
    @PreAuthorize("@ss.hasPerm('sale:contract:commit')")
    @Log(title = "销售合同", businessType = BusinessType.OTHER)
    @PostMapping("/commit")
    public AjaxResult commit(@RequestBody Long[] contractIds) {
        return toAjax(contractService.commitContract(Arrays.asList(contractIds)));
    }

    /**
     * 撤回销售合同
     *
     * @param contractIds 销售合同ID数组
     * @return 结果
     */
    @PreAuthorize("@ss.hasPerm('sale:contract:recall')")
    @Log(title = "销售合同", businessType = BusinessType.OTHER)
    @PostMapping("/recall")
    public AjaxResult recall(@RequestBody Long[] contractIds) {
        return toAjax(contractService.recallContract(Arrays.asList(contractIds)));
    }

    /**
     * 审批销售合同
     *
     * @param contract 销售合同
     * @return 结果
     */
    @PreAuthorize("@ss.hasPerm('sale:contract:approve')")
    @Log(title = "销售合同", businessType = BusinessType.OTHER)
    @PostMapping("/approve")
    public AjaxResult approve(@RequestBody SalContract contract) {
        return toAjax(contractService.approveContract(contract));
    }

    /**
     * 弃审销售合同
     *
     * @param contract 销售合同
     * @return 结果
     */
    @PreAuthorize("@ss.hasPerm('sale:contract:unapprove')")
    @Log(title = "销售合同", businessType = BusinessType.OTHER)
    @PostMapping("/unapprove")
    public AjaxResult unapprove(@RequestBody SalContract contract) {
        return toAjax(contractService.unapproveContract(contract));
    }
}
